package com.cxm.weilai.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Chenman
 * @create 2019/6/27
 * @since 1.0.0
 */
public class OkHttpRequestDemo {

    public static String getDemo() {
        //返回的accessToken:String类型的Json串,需解析Json才能拿到里面的accessToken
        String accessToken = "";
        OkHttpRequest okHttpRequest = new OkHttpRequest();
        //生成accessToken的接口URL
        String accessTokenUrl = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=NAmlbefQGtKtMMfy3YWnajkW&client_secret=EOQdObwV6Sg9sTxccQPCLXWq4watPnXi";
        try {
            //发送请求
            accessToken = okHttpRequest.get(accessTokenUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return accessToken;
    }

    public static String postJsonDemo() {
        //返回的accessToken:String类型的Json串,需解析Json才能拿到里面的accessToken
        String accessToken = "";
        OkHttpRequest okHttpRequest = new OkHttpRequest();
        //生成accessToken的接口URL
        String url = "http://smssh1.253.com/msg/send/json";
        String json = "{\n" +
                "    \"account\":\"N3130706\",\n" +
                "    \"password\":\"UGo7qNaEpsa6f8\",\n" +
                "    \"msg\":\"你好\",\n" +
                "    \"phone\":\"18736633947\"\n" +
                "}";
        try {
            //发送请求
            accessToken = okHttpRequest.postJson(url,json);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return accessToken;
    }

    public static String postFormDataDemo() {
        String accessToken = "";
        OkHttpRequest okHttpRequest = new OkHttpRequest();
        String url = "https://api.253.com/open/bankcard/card-auth";
        Map<String, String> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("appId","RpvvHMuD");
        objectObjectHashMap.put("appKey","XKUUGvnZ");
        objectObjectHashMap.put("name","谢永林");
        objectObjectHashMap.put("idNum","342623199108251417");
        objectObjectHashMap.put("cardNo","6222620110039462883");
        try {
            //发送请求
            accessToken = okHttpRequest.postFormData(url,objectObjectHashMap);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return accessToken;
    }

    public static void main(String[] args) {
        String getDemo = getDemo();
        String postJsonDemo = postJsonDemo();
        String postFormDataDemo = postFormDataDemo();
        System.out.println("getDemo:"+getDemo);
        System.out.println("postJsonDemo:"+postJsonDemo);
        System.out.println("postFormDataDemo:"+postFormDataDemo);
    }
}